﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;

namespace RentalSystem.Data.EF.Dao
{
    public class EFCustomersDao : ICustomersDao
    {
        #region Queries

        private static string GetQuery;

        #endregion

        private EFManager<Customers> manager;

        public EFCustomersDao(RentalDatabaseEntities repository)
        {
            GetQuery = "SELECT VALUE c FROM Customers AS c WHERE c.CustomerGuid = @Guid;";
            this.manager = new EFManager<Customers>(repository);
        }

        #region IDao1Key

        public Customers Add(Customers entity)
        {
            manager.Database.AddToCustomers(entity);
            manager.Save();
            return entity;
        }

        public Customers Get(string id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Guid", id)
                };

            var customers = manager.SQLQueryToEntity(GetQuery, parameters);

            foreach(Customers customer in customers)
            {
                return customer;
            }

            return null;
        }

        public void Delete(string id)
        {
            throw new NotImplementedException();
        }

        public void Delete(Customers entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Customers entity, uint fields)
        {
             Customers fromDb = Get(entity.CustomerGuid);

             if (fromDb != null)
             {
                 if ((fields & (uint)CustomerFields.CustomerType) == (uint)CustomerFields.CustomerType)
                     fromDb.CustomerTypeID = entity.CustomerTypeID;
                 if ((fields & (uint)CustomerFields.Person) == (uint)CustomerFields.Person)
                     fromDb.PersonGuid = entity.PersonGuid;
             }
                manager.Save();
        }

        public List<Customers> List()
        {
            throw new NotImplementedException();
        }
        
        #endregion
    }
}
